import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:kxy_flutter_app/components/empty_widget.dart';
import 'package:kxy_flutter_app/model/exam_question.dart';
import 'package:kxy_flutter_app/view/examination/widget/question_result_card_widget.dart';
import 'package:kxy_flutter_app/view/examination/widget/question_swiper_widget.dart';
import 'package:kxy_flutter_app/view/examination/widget/review_bottom_bar_widget.dart';
import 'widget/question_header_widget.dart';

class DoReviewView extends StatefulWidget {
  final String title;
  final List<Question> quesList;
  final List<Widget> actions;
  final bool readonly;
  final int initIndex;
  final bool withQuesCard;
  final bool withCollect;

  /* 分页加载时使用 */
  final int totalCount;
  final Function loadMore;
  final Function(int) onChange;

  const DoReviewView({
    Key key,
    @required this.title,
    @required this.quesList,
    this.readonly = true,
    this.initIndex = 0,
    this.withQuesCard = false,
    this.withCollect = false,
    this.actions,

    /* 分页加载时需要 */
    this.loadMore,
    this.totalCount = 0,
    this.onChange,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _DoReviewViewState();
  }
}

class _DoReviewViewState extends State<DoReviewView> {
  List<Question> quesList;
  int index = 0;
  int totalCount = 0;
  SwiperController swiperCtrl = new SwiperController();
  List<Widget> appbarActioins = new List<Widget>();

  Question get current {
    if (quesList.length < index) return null;
    return quesList[index];
  }

  @override
  void initState() {
    super.initState();

    /* 导航菜单 */
    if (widget.withQuesCard) {
      appbarActioins.add(quesCardBtn());
    }
    if (widget.actions != null) {
      widget.actions.forEach((item) {
        appbarActioins.add(item);
      });
    }

    /* 答题状态 */
    if (widget.readonly) {
      widget.quesList.forEach((item) => item.confirmed = true);
    }

    quesList = widget.quesList;

    /* 初始化页面index */
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      if (widget.initIndex > 0) {
        swiperCtrl.move(widget.initIndex, animation: false);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    if (widget.totalCount == 0) {
      totalCount = widget.quesList.length;
    } else {
      totalCount = widget.totalCount;
    }

    /* 分页需要重新赋值 */
    if (widget.onChange != null) {
      quesList = widget.quesList;
    }

    /* 防止超出数组界限 */
    if (index >= quesList.length) {
      index = quesList.length - 1;
      swiperCtrl.move(index);
    }

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        elevation: 0,
        title: Text(widget.title),
        actions: appbarActioins,
      ),
      body: body(),
    );
  }

  Widget quesCardBtn() {
    return FlatButton(
      child: Text('答题卡'),
      onPressed: () {
        showDialog(
          context: context,
          builder: (cxt) => QuestionResultCardWidget(
            quesList: widget.quesList,
            onSelect: (index) {
              swiperCtrl.move(index, animation: false);
            },
          ),
        );
      },
    );
  }

  Widget body() {
    if (quesList.length == 0) {
      return EmptyWidget(onRefresh: widget.loadMore);
    }
    return Column(
      children: [
        QuestionHeaderWidget(
          index: index,
          total: totalCount,
          question: quesList[index],
        ),
        Expanded(
          child: QuestionSwiperWidget(
            swiperCtrl: swiperCtrl,
            questionList: quesList,
            setAnswer: (answer) {
              if (widget.readonly) return;
              if (current == null) return;
              if (current.confirmed) return;
              setState(() {
                current.userAnswer = answer;
              });
            },
            withResult: true,
            onChangeIndex: onIndexChange,
          ),
        ),
        ReviewBottomBarWidget(
          swiperCtrl: swiperCtrl,
          index: index,
          totalCount: totalCount,
          question: quesList[index],
          withCollect: widget.withCollect,
          complete: () {
            if (widget.readonly) return;
            if (current == null) return;
            setState(() {
              current.confirmed = true;
            });
          },
        ),
      ],
    );
  }

  void onIndexChange(index) {
    if (widget.onChange != null) {
      widget.onChange(index);
    }
    if (quesList.length < totalCount) {
      if (index > quesList.length - 10) {
        widget.loadMore();
      }
    }
    setState(() {
      this.index = index;
    });
  }
}
